﻿using DigDes.DSchool.SUPS.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlTypes;

namespace DigDes.DSchool.SUPS.DataAccess.Database
{
    public class FileLoaderDAC
    {

        private static readonly string ConnectionStringKey = "SUPSDB";

        public static void LoadRowToDB(CarInfo row)
        {
            var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringKey].ConnectionString;
            using (var sqlConn = new SqlConnection(connectionString))
            {
                using (var sqlCmd = sqlConn.CreateCommand())
                {
                    sqlCmd.CommandText = "INSERT INTO SomeTable values " + 
                                        "(@carNumber, @cargoWeight, @destinationStation, @cargoCode, @consigneeCode, @carParkType, @departureStation, @operationCode, @operationTime, @stationOfOperation, " +
                                         "@deliveryRoad, @receipRoad, @trainIndex, @trainNumber)";
                    
                    SqlDateTime time = new SqlDateTime(row.OperationTime);

                    sqlCmd.Parameters.AddWithValue("@carNumber", row.CarNumber);
                    sqlCmd.Parameters.AddWithValue("@cargoWeight", row.CargoWeight);
                    sqlCmd.Parameters.AddWithValue("@destinationStation", row.DestinationStation);
                    sqlCmd.Parameters.AddWithValue("@cargoCode", row.CargoCode);
                    sqlCmd.Parameters.AddWithValue("@consigneeCode", row.ConsigneeCode);
                    sqlCmd.Parameters.AddWithValue("@carParkType", row.CarParkType);
                    sqlCmd.Parameters.AddWithValue("@departureStation", row.DepartureStation);
                    sqlCmd.Parameters.AddWithValue("@operationCode", row.OperationCode);
                    sqlCmd.Parameters.AddWithValue("@operationTime", time);
                    sqlCmd.Parameters.AddWithValue("@stationOfOperation", row.StationOfOperation);
                    sqlCmd.Parameters.AddWithValue("@deliveryRoad", row.DeliveryRoad);
                    sqlCmd.Parameters.AddWithValue("@receipRoad", row.ReceipRoad);
                    sqlCmd.Parameters.AddWithValue("@trainIndex", row.TrainIndex);
                    sqlCmd.Parameters.AddWithValue("@trainNumber", row.TrainNumber);

                    sqlConn.Open();

                    sqlCmd.ExecuteNonQuery();
                }
            }
        }
    }
}
